<?php
class AdminAction extends Action {
	public function index() {
		$this->assign ( 'title', '羽毛球积分赛 - 管理' );
		
		// 判断用户登录信息
		$this->assign ( 'admin', 'none' );
		if (session ( 'username' )) {
			$this->assign ( 'loginuser', session ( 'username' ) );
			$this->assign ( 'userscore', session ( 'userscore' ) );
			$this->assign ( 'usermatchcount', session ( 'usermatchcount' ) );
			$this->assign ( 'loginusercolor', get_color ( session ( 'usergender' ) ) );
			
			if (intval ( session ( 'userlevel' ) ) < 1) {
				$this->assign ( 'userstatus', '（未认证）' );
			} else {
				$this->assign ( 'userstatus', '' );
			}
			
			if (intval ( session ( 'userlevel' ) ) > 1) {
				$this->assign ( 'admin', 'inline' );
			}
			
			$this->assign ( 'logoffblock', 'inline' );
			$this->assign ( 'loginblock', 'none' );
		} else {
			$this->assign ( 'logoffblock', 'none' );
			$this->assign ( 'loginblock', 'inline' );
		}
		
		$this->display ();
	}
	
	public function recalcmatch() {
		$matches = M ( 'MatchRecords' );
		$match_data = $matches->field ( 'recorder', true )->order ( 'match_date, match_id' )->select ();
		// dump ( $match_data );
		
		$users = M ( 'Users' );
		$user_data = $users->where ( 'level>0' )->field ( 'username,password,level,register_time,last_visit_time,last_visit_ip', true )->select ();
		// dump ( $user_data );
		for($i = 0; $i < count ( $user_data ); $i ++) {
			$user_data [$i] ['single_score'] = 1000;
			$user_data [$i] ['double_score'] = 1000;
			$user_data [$i] ['small_score'] = 1000;
			$user_data [$i] ['single_match_count'] = 0;
			$user_data [$i] ['double_match_count'] = 0;
			$user_data [$i] ['small_match_count'] = 0;
			$user_data [$i] ['single_win_count'] = 0;
			$user_data [$i] ['double_win_count'] = 0;
			$user_data [$i] ['small_win_count'] = 0;
			$user_data [$i] ['last_single_match_date'] = 0;
			$user_data [$i] ['last_double_match_date'] = 0;
			$user_data [$i] ['last_small_match_date'] = 0;
			
			$user_index [$user_data [$i] ['user_id']] = $i;
		}
		// dump ( $user_index );
		
		for($i = 0; $i < count ( $match_data ); $i ++) {
			if ($match_data [$i] ['match_rule'] != '11scores') {
				$this->error ( '数据不合法！match_rule=' . $match_data [$i] ['match_rule'] );
			}
			
			if ($match_data [$i] ['match_type'] == 'single') {
				$match_data [$i] ['ori_score_pros_player1'] = $user_data [$user_index [$match_data [$i] ['pros_player1']]] ['single_score'];
				$match_data [$i] ['ori_score_cons_player1'] = $user_data [$user_index [$match_data [$i] ['cons_player1']]] ['single_score'];
				if (0 != get_single_match_result ( $match_data [$i] ['match_coef'], $match_data [$i] ['pros_score'], $match_data [$i] ['cons_score'], $user_data [$user_index [$match_data [$i] ['pros_player1']]] ['single_score'], $user_data [$user_index [$match_data [$i] ['cons_player1']]] ['single_score'] )) {
					$this->error ( '计算错误！match_id=' . $match_data [$i] ['match_id'] );
				}
				$match_data [$i] ['var_score_pros_player1'] = $user_data [$user_index [$match_data [$i] ['pros_player1']]] ['single_score'] - $match_data [$i] ['ori_score_pros_player1'];
				$match_data [$i] ['var_score_cons_player1'] = $user_data [$user_index [$match_data [$i] ['cons_player1']]] ['single_score'] - $match_data [$i] ['ori_score_cons_player1'];
				
				$user_data [$user_index [$match_data [$i] ['pros_player1']]] ['single_match_count'] ++;
				$user_data [$user_index [$match_data [$i] ['cons_player1']]] ['single_match_count'] ++;
				
				if ($match_data [$i] ['pros_score'] > $match_data [$i] ['cons_score']) {
					$user_data [$user_index [$match_data [$i] ['pros_player1']]] ['single_win_count'] ++;
				} else {
					$user_data [$user_index [$match_data [$i] ['cons_player1']]] ['single_win_count'] ++;
				}
				
				$user_data [$user_index [$match_data [$i] ['pros_player1']]] ['last_single_match_date'] = $match_data [$i] ['match_date'];
				$user_data [$user_index [$match_data [$i] ['cons_player1']]] ['last_single_match_date'] = $match_data [$i] ['match_date'];
			} else if ($match_data [$i] ['match_type'] == 'double') {
				$match_data [$i] ['ori_score_pros_player1'] = $user_data [$user_index [$match_data [$i] ['pros_player1']]] ['double_score'];
				$match_data [$i] ['ori_score_pros_player2'] = $user_data [$user_index [$match_data [$i] ['pros_player2']]] ['double_score'];
				$match_data [$i] ['ori_score_cons_player1'] = $user_data [$user_index [$match_data [$i] ['cons_player1']]] ['double_score'];
				$match_data [$i] ['ori_score_cons_player2'] = $user_data [$user_index [$match_data [$i] ['cons_player2']]] ['double_score'];
				if (0 != get_double_match_result ( $match_data [$i] ['match_coef'], $match_data [$i] ['pros_score'], $match_data [$i] ['cons_score'], $user_data [$user_index [$match_data [$i] ['pros_player1']]] ['double_score'], $user_data [$user_index [$match_data [$i] ['pros_player2']]] ['double_score'], $user_data [$user_index [$match_data [$i] ['cons_player1']]] ['double_score'], $user_data [$user_index [$match_data [$i] ['cons_player2']]] ['double_score'] )) {
					$this->error ( '计算错误！match_id=' . $match_data [$i] ['match_id'] );
				}
				$match_data [$i] ['var_score_pros_player1'] = $user_data [$user_index [$match_data [$i] ['pros_player1']]] ['double_score'] - $match_data [$i] ['ori_score_pros_player1'];
				$match_data [$i] ['var_score_pros_player2'] = $user_data [$user_index [$match_data [$i] ['pros_player2']]] ['double_score'] - $match_data [$i] ['ori_score_pros_player2'];
				$match_data [$i] ['var_score_cons_player1'] = $user_data [$user_index [$match_data [$i] ['cons_player1']]] ['double_score'] - $match_data [$i] ['ori_score_cons_player1'];
				$match_data [$i] ['var_score_cons_player2'] = $user_data [$user_index [$match_data [$i] ['cons_player2']]] ['double_score'] - $match_data [$i] ['ori_score_cons_player2'];
				
				$user_data [$user_index [$match_data [$i] ['pros_player1']]] ['double_match_count'] ++;
				$user_data [$user_index [$match_data [$i] ['pros_player2']]] ['double_match_count'] ++;
				$user_data [$user_index [$match_data [$i] ['cons_player1']]] ['double_match_count'] ++;
				$user_data [$user_index [$match_data [$i] ['cons_player2']]] ['double_match_count'] ++;
				
				if ($match_data [$i] ['pros_score'] > $match_data [$i] ['cons_score']) {
					$user_data [$user_index [$match_data [$i] ['pros_player1']]] ['double_win_count'] ++;
					$user_data [$user_index [$match_data [$i] ['pros_player2']]] ['double_win_count'] ++;
				} else {
					$user_data [$user_index [$match_data [$i] ['cons_player1']]] ['double_win_count'] ++;
					$user_data [$user_index [$match_data [$i] ['cons_player2']]] ['double_win_count'] ++;
				}
				
				$user_data [$user_index [$match_data [$i] ['pros_player1']]] ['last_double_match_date'] = $match_data [$i] ['match_date'];
				$user_data [$user_index [$match_data [$i] ['pros_player2']]] ['last_double_match_date'] = $match_data [$i] ['match_date'];
				$user_data [$user_index [$match_data [$i] ['cons_player1']]] ['last_double_match_date'] = $match_data [$i] ['match_date'];
				$user_data [$user_index [$match_data [$i] ['cons_player2']]] ['last_double_match_date'] = $match_data [$i] ['match_date'];
			} else if ($match_data [$i] ['match_type'] == 'small') {
				$match_data [$i] ['ori_score_pros_player1'] = $user_data [$user_index [$match_data [$i] ['pros_player1']]] ['small_score'];
				$match_data [$i] ['ori_score_cons_player1'] = $user_data [$user_index [$match_data [$i] ['cons_player1']]] ['small_score'];
				if (0 != get_single_match_result ( $match_data [$i] ['match_coef'], $match_data [$i] ['pros_score'], $match_data [$i] ['cons_score'], $user_data [$user_index [$match_data [$i] ['pros_player1']]] ['small_score'], $user_data [$user_index [$match_data [$i] ['cons_player1']]] ['small_score'] )) {
					$this->error ( '计算错误！match_id=' . $match_data [$i] ['match_id'] );
				}
				$match_data [$i] ['var_score_pros_player1'] = $user_data [$user_index [$match_data [$i] ['pros_player1']]] ['small_score'] - $match_data [$i] ['ori_score_pros_player1'];
				$match_data [$i] ['var_score_cons_player1'] = $user_data [$user_index [$match_data [$i] ['cons_player1']]] ['small_score'] - $match_data [$i] ['ori_score_cons_player1'];
				
				$user_data [$user_index [$match_data [$i] ['pros_player1']]] ['small_match_count'] ++;
				$user_data [$user_index [$match_data [$i] ['cons_player1']]] ['small_match_count'] ++;
				
				if ($match_data [$i] ['pros_score'] > $match_data [$i] ['cons_score']) {
					$user_data [$user_index [$match_data [$i] ['pros_player1']]] ['small_win_count'] ++;
				} else {
					$user_data [$user_index [$match_data [$i] ['cons_player1']]] ['small_win_count'] ++;
				}
				
				$user_data [$user_index [$match_data [$i] ['pros_player1']]] ['last_small_match_date'] = $match_data [$i] ['match_date'];
				$user_data [$user_index [$match_data [$i] ['cons_player1']]] ['last_small_match_date'] = $match_data [$i] ['match_date'];
			} else {
				$this->error ( '数据不合法！match_type=' . $match_data [$i] ['match_type'] );
			}
		}
		
		for($i = 0; $i < count ( $user_data ); $i ++) {
			$user_data [$i] ['total_score'] = $user_data [$i] ['single_score'] + $user_data [$i] ['double_score'] + $user_data [$i] ['small_score'];
			$user_data [$i] ['total_match_count'] = $user_data [$i] ['single_match_count'] + $user_data [$i] ['double_match_count'] + $user_data [$i] ['small_match_count'];
		}
		
		// dump ( $user_data );
		for($i = 0; $i < count ( $user_data ); $i ++) {
			$users->where ( 'user_id=' . $user_data [$i] ['user_id'] )->save ( $user_data [$i] );
		}
		
		// dump ( $match_data );
		for($i = 0; $i < count ( $match_data ); $i ++) {
			$matches->where ( 'match_id=' . $match_data [$i] ['match_id'] )->save ( $match_data [$i] );
		}
		
		$this->success ( "操作成功" );
	}
	
	public function recalcfunds() {
		$acts = M ( 'ActRecords' );
		$records_data = $acts->field ( 'recorder', true )->order ( 'date' )->select ();
		//dump ( $records_data );
		
		$users = M ( 'Users' );
		$user_data = $users->field ( 'user_id,balance,total_in,total_out,recent_act_date,change_count,init_in,init_out' )->select ();
		//dump ( $user_data );
		
		for($i = 0; $i < count ( $user_data ); $i ++) {
			$user_id [$user_data [$i] ['user_id']] = $i;
			$user_data [$i] ['balance'] = $user_data [$i] ['init_in'] + $user_data [$i] ['init_out'];
			$user_data [$i] ['total_in'] = $user_data [$i] ['init_in'];
			$user_data [$i] ['total_out'] = $user_data [$i] ['init_out'];
			$user_data [$i] ['change_count'] = 0;
		}
		
		for($i = 0; $i < count ( $records_data ); $i ++) {
			$ids = split ( ",", $records_data [$i] ['participants'] );
			foreach ( $ids as $id ) {
				$k = $user_id [$id];
				$user_data [$k] ['balance'] += $records_data [$i] ['avg_cost'];
				if ($records_data [$i] ['avg_cost'] > 0) {
					$user_data [$k] ['total_in'] += $records_data [$i] ['avg_cost'];
				} else {
					$user_data [$k] ['total_out'] += $records_data [$i] ['avg_cost'];
				}
				if($records_data [$i] ['is_act'] == 1)
				{
					$user_data [$k] ['change_count'] ++ ;
					$user_data [$k]['recent_act_date'] = $records_data[$i]['date'];
				}
			}
		}
		//dump ( $user_data );
		
		for($i = 0; $i < count ( $user_data ); $i ++) {
			$user_data [$i]['balance']= round($user_data [$i]['balance'], 2);
			$users->where ( 'user_id=' . $user_data [$i] ['user_id'] )->save ( $user_data [$i] );
		}
		
		$this->success ( "操作成功" );
	}
	
	public function grant() {
		$this->assign ( 'title', '羽毛球积分赛 - 新用户授权' );
		
		$users = M ( 'Users' );
		$data = $users->where ( 'level<=0' )->order ( 'user_id' )->field ( 'user_id,username,register_time,last_visit_time,last_visit_ip' )->select ();
		$this->assign ( 'userlist', $data );
		
		// 判断用户登录信息
		$this->assign ( 'admin', 'none' );
		if (session ( 'username' )) {
			$this->assign ( 'loginuser', session ( 'username' ) );
			$this->assign ( 'userscore', session ( 'userscore' ) );
			$this->assign ( 'usermatchcount', session ( 'usermatchcount' ) );
			$this->assign ( 'loginusercolor', get_color ( session ( 'usergender' ) ) );
			
			if (intval ( session ( 'userlevel' ) ) < 1) {
				$this->assign ( 'userstatus', '（未认证）' );
			} else {
				$this->assign ( 'userstatus', '' );
			}
			
			if (intval ( session ( 'userlevel' ) ) > 1) {
				$this->assign ( 'admin', 'inline' );
			}
			
			$this->assign ( 'logoffblock', 'inline' );
			$this->assign ( 'loginblock', 'none' );
		} else {
			$this->assign ( 'logoffblock', 'none' );
			$this->assign ( 'loginblock', 'inline' );
		}
		
		$this->display ();
	}
	
	public function approve() {
		if (count ( $_POST ['user_ids'] ) <= 0) {
			$this->error ( "未选择授权用户" );
		}
		$users = M ( 'Users' );
		foreach ( $_POST ['user_ids'] as $value ) {
			$users->where ( 'user_id=' . $value )->setField ( 'level', 1 );
		}
		$this->success ( '授权成功' );
	}
	
	public function del() {
		$id = $_GET ['id'];
		$users = M ( 'Users' );
		if ($users->delete ( $id )) {
			$this->success ( '删除成功' );
		} else {
			$this->error ( '删除失败' );
		}
	}
}